import fsextra from "fs-extra";
import path from "path";
import { promises } from "fs";
import { processcssminify } from "./processcssminify";

export async function savepagedatacss(
    url: string,
    pagepath: string,
    cachestylecssrules: Map<string, Set<string>>
) {
    const key = url;
    //@ts-ignore
    let filename = btoa(key) + ".css";
    let mdfilepath = path.join(pagepath, filename);

    const css = Array.from(cachestylecssrules.get(url) || new Set()).join("\n");
    const cssmini =
        process.env.NODE_ENV === "development"
            ? css
            : await processcssminify(css);
    const serilizedtext = cssmini;

    await fsextra.ensureFile(mdfilepath);
    await promises.writeFile(mdfilepath, serilizedtext);
}
